package com.xlx.ui.controller;

import com.alibaba.fastjson.JSONObject;
import com.xlx.ui.domain.MO.File;
import com.xlx.ui.domain.VO.ResultBody;
import com.xlx.ui.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @author: 张川
 * @date: 2020-12-01 22:08
 */
@Api(tags = "文件资源")
@RestController
@RequestMapping("/common")
public class CommonController {

    FileService fileService;

    public CommonController(FileService fileService) {
        this.fileService = fileService;
    }

    @ApiOperation(value = "获取文件列表")
    @GetMapping("/getall")
    public ResultBody getall(String dir) {
        return ResultBody.success(fileService.list(dir));
    }

    @ApiOperation(value = "搜索文件")
    @GetMapping("/search")
    public ResultBody search(String dir, String fileName) {
        return ResultBody.success(fileService.search(dir, fileName));
    }

    @ApiOperation(value = "预览文件")
    @GetMapping("/preview")
    public ResultBody preview(String fileName) {
        return ResultBody.success(fileService.preview(fileName));
    }


    @ApiOperation(value = "创建文件夹")
    @GetMapping("/creat")
    public ResultBody creat(String dir, String name) {
        return fileService.creat(dir, name) ? ResultBody.success() : ResultBody.error();
    }

    @ApiOperation(value = "下载文件")
    @GetMapping("/down")
    public ResultBody down(String name, String title) {
        return ResultBody.success(fileService.down(name, title));
    }


    @ApiOperation(value = "上传文件")
    @GetMapping("/getpolicy")
    public ResultBody getpolicy(String dir) {
        return ResultBody.success(fileService.upload(dir));
    }

    @ApiOperation(value = "删除文件")
    @GetMapping("/delete")
    public ResultBody callbackFn(String dir) {
        return fileService.delete(dir) ? ResultBody.success() : ResultBody.error();
    }

    @ApiOperation(value = "移动文件")
    @GetMapping("/move")
    public ResultBody move(String moveFile, String baseDir) {
        return fileService.move(moveFile, baseDir) ? ResultBody.success() : ResultBody.error();
    }


    @ApiOperation(value = "添加需要审核的文件")
    @PostMapping("/examine")
    public ResultBody examine(@RequestBody JSONObject jsonObject) {
        return fileService.addExamine(jsonObject.getString("name"),jsonObject.getString("path"),
                jsonObject.getString("file_name"),0);
    }
    @ApiOperation(value = "查询所有文件记录")
    @GetMapping("/examine/all")
    public ResultBody all(Integer page,Integer limit,String name,String fileName){
        List<File> fileList = fileService.getExamine(page,limit,name,fileName);
        JSONObject response = new JSONObject();
        response.put("items",fileList);
        response.put("total",fileList.size());
        return ResultBody.success(response);
    }

    @ApiOperation(value = "修改文件状态")
    @PostMapping("/examine/update")
    public ResultBody examine_update(@RequestBody JSONObject jsonObject){
        File file = new File();
        file.setId(jsonObject.getInteger("id"));
        file.setExamine(jsonObject.getInteger("state"));
        fileService.update(file);
        return ResultBody.success();
    }

}
